﻿package sprites {
	import astar.AStar;
	import astar.Node;
	import org.flixel.data.FlxPanel;
	import sprites.Fire;
	import org.flixel.*;
	import states.LevelState;

	/**
	 * ...
	 * @author test
	 */
	public class Sheeps extends FlxGroup {
		public var cur_sheep_num:uint;
		public var _player:Player;
		public var _path_finder:AStar;
		public var _state:LevelState;
		public var ready:Boolean = new Boolean(false);

		override public function update():void {
			super.update();
			if (ready){
				if (members[cur_sheep_num].is_blocked == false){
					if (cur_sheep_num != members.length - 1){
						var ns:Sheep = members[cur_sheep_num + 1];
						var cs:Sheep = members[cur_sheep_num ]
						_path_finder.start(Utils.generalToTile(cs.x, cs.y), Utils.generalToTile(ns.x, ns.y));
						cs.path = _path_finder.path;
						_state.show_path(_path_finder.path);
						
						//cs.go_along_path();
						var np:Node = _path_finder.path[1];
						var npp:FlxPoint = Utils.tileToGeneral(np.x, np.y);
						cs.x = npp.x;
						cs.y = npp.y;
						
						cur_sheep_num++;
					}
				}
			}
		}

		public function Sheeps(){
			super();
		}

		public function add_sheep(_s:Sheep):void {
			this.add(_s);
		}

		public function start():void {

			_player = _state._player;
			_path_finder = _state.path_finder;

			for each (var _s:Sheep in this.members){
				_s.dis = new Number(Math.pow(_s.x - _player.x, 2) + Math.pow(_s.y - _player.y, 2));
				_s._player = _player;
			}
			this.members.sort(Sheep.compare);

			cur_sheep_num = new uint(0);

			ready = true;
		}
	}

}